package com.xing.libao.init;

import com.xing.libao.util.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: xingzhe
 * Date: 2015/8/4
 * Time: 17:06
 */
public class CLImage {
    public static void main(String[] args) throws IOException{
//        RequestUtil requestUtil = RequestUtil.getInstance();
//        for(int i=1;i<5;i++){
//            String url = "http://dz.cldz.biz/thread0806.php?fid=15&page=" + i;
//            String content = requestUtil.getHtml(url);
//            if (StringUtils.isEmpty(content))
//                continue;
//            Document document = Jsoup.parse(content);
//            Elements elements = document.select("h3>a");
//            for (Element element : elements) {
//                String href = element.attr("href");
//                if (href.contains("htm_data")) {
//                    System.out.println("http://dz.cldz.biz/" + href);
//                    content = requestUtil.getHtml("http://dz.cldz.biz/" + href);
//                    if (StringUtils.isEmpty(content))
//                        continue;
//                    String title = getPattern("<b>本頁主題:</b> (.*?)</td>", content);
//                    title = title.replaceAll("-","").replaceAll("\\[.*?\\]", "");
//                    String fan = title.substring(0, 6);
//                    System.out.println(fan);
//                    Pattern pattern = Pattern.compile("<img src='(.*?)'", Pattern.CASE_INSENSITIVE);
//                    Matcher matcher = pattern.matcher(content);
//                    while (matcher.find()) {
//                        String group = matcher.group(1);
//                        if (group.contains(fan.toLowerCase())) {
//                            String fileName = group.substring(group.lastIndexOf("/") + 1, group.length());
//                            System.out.println(group);
//                            File file = new File("D:\\torrent\\" + fileName);
//                            if (!file.exists()) {
//                                if (file.createNewFile()) {
//                                    CloseableHttpResponse response = requestUtil.execute(new HttpGet(group));
//                                    if (response != null) {
//                                        BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(file));
//                                        byte[] result = EntityUtils.toByteArray(response.getEntity());
//                                        bw.write(result);
//                                        bw.close();
//                                        response.close();
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
        try {
            readContent("http://dz.cldz.biz/htm_data/15/1508/1588598.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void readContent(String contentUrl) throws Exception{
        RequestUtil requestUtil = RequestUtil.getInstance();
        System.out.println(contentUrl);
        String content = requestUtil.getHtml(contentUrl);
        System.out.println(content);
        String title = getPattern("<b>本頁主題:</b> (.*?)</td>", content);
        System.out.println(title);
        title = title.replaceAll("-","").replaceAll("\\[.*?\\]", "");
        String fan = title.substring(0, 6);
        System.out.println(fan);
        Pattern pattern = Pattern.compile("<img src='(.*?)'", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String group = matcher.group(1);
            if (group.contains(fan.toLowerCase())) {
                String fileName = group.substring(group.lastIndexOf("/") + 1, group.length());
                System.out.println(group);
                File file = new File("D:\\torrent\\" + fileName);
                if (!file.exists()) {
                    if (file.createNewFile()) {
                        CloseableHttpResponse response = requestUtil.execute(new HttpGet(group));
                        if (response != null) {
                            BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(file));
                            byte[] result = EntityUtils.toByteArray(response.getEntity());
                            bw.write(result);
                            bw.close();
                            response.close();
                        }
                    }
                }
            }
        }
    }

    private static String getPattern(String regex, String content) {
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        String title = null;
        if (matcher.find()) {
            title = matcher.group(1);
        } else {
            System.out.println("pattern can't find!" + regex);
        }
        return title;
    }
}
